﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RRTWSEntityDSD.Dominio;
using System.Data.SqlClient;
using System.Data;
using WebServiceDSDG6.Persistencia;

namespace RRTWSNegocioDSD.Dao
{
    public class UbigeoDAO : BaseDAO<Ubigeo, int>
    {
        public List<Ubigeo> getListarUbigeoPorNombre(string inicialnombre)
        {
            try
            {
                List<Ubigeo> listado = new List<Ubigeo>();
                using (SqlCommand cmd = new SqlCommand("TRI_ssf_Ubigeo", cnx))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UBIG_NOMBRE", inicialnombre);
                    cnx.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            if (!Convert.IsDBNull(dr["UBIG_IDUBIGEO"]))
                            {
                                Ubigeo ubigeo = new Ubigeo();
                                ubigeo.idubigeo = int.Parse(dr["UBIG_IDUBIGEO"].ToString());
                                ubigeo.nombre = dr["UBIG_NOMBRE"].ToString();
                                ubigeo.codigo = dr["UBIG_CODIGO"].ToString();
                                listado.Add(ubigeo);
                            }
                        }
                    }
                    return listado;
                }
            }
            catch (InvalidOperationException e)
            {
                throw new InvalidOperationException("Error de conexiones : " + e.Message);
            }
            finally
            {
                cnx.Close();
            }
            return null;
        }

        public Ubigeo ObtenerPorCodUbigeo(string Codigo)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand("TRI_sk_Ubigeo", cnx))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UBIG_CODIGO", Codigo);
                    cnx.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            if (!Convert.IsDBNull(dr["UBIG_IDUBIGEO"]))
                            {
                                Ubigeo ubigeo = new Ubigeo();
                                ubigeo.idubigeo = int.Parse(dr["UBIG_IDUBIGEO"].ToString());
                                ubigeo.nombre = dr["UBIG_NOMBRE"].ToString();
                                ubigeo.codigo = dr["UBIG_CODIGO"].ToString();
                                return ubigeo;
                            }
                        }
                    }
                }
            }
            catch (InvalidOperationException e)
            {
                throw new InvalidOperationException("Error de conexiones : " + e.Message);
            }
            finally
            {
                cnx.Close();
            }
            return null;
        }


    }
}